package xjtu.zd.server.user.myself;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xjtu.zd.server.common.CommonService;
import xjtu.zd.server.common.CurrentUser;
import xjtu.zd.server.common.Result;
import xjtu.zd.server.common.TokenUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/myself")
public class RUserAPI {
    @Resource
    private CommonService commonService;

    @Resource
    private RUserService userService;

    @GetMapping("/home")
    public Result getOneData(@RequestHeader("Token") String token){
        //CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        return Result.success(userService.getNowUser("2001"));
    }
    @GetMapping("/bookshelf/{u_id}")
    public Result getBookShelf(@PathVariable String u_id){
        return Result.success(userService.bookShelf(u_id));
    }
    @GetMapping("/works/{u_id}")
    public Result getSubscribe(@PathVariable String u_id){
        return Result.success(userService.getWorks(u_id));
    }

    @GetMapping("/avatar/{filename}")
    public void getAvator(@PathVariable String filename, HttpServletResponse response) throws IOException {

        //读取文件的输入流
        InputStream in = new FileInputStream(CurrentUser.AVATAR_DIR+"/"+filename);
        //将文件信息输出到浏览器的输出流
        OutputStream out = response.getOutputStream();

        byte[] b = new byte[10240];//容量为10240字节的缓存区
        int len = -1;//从输入流读取并放入缓存区的数据量

        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.flush();
        out.close();
        in.close();
    }

    @PostMapping("/avatar")
    public Result uploadAvaor(MultipartFile avatar, @RequestHeader("Token") String token) throws IOException {

        InputStream in = avatar.getInputStream();

        String name = UUID.randomUUID().toString();//获得通过uuid算法得到的不重复的字符串
        String submitedFilename = avatar.getOriginalFilename();//原始文件名
        String suffix = submitedFilename.substring(submitedFilename.lastIndexOf("."));
        String filename = name+suffix;
        OutputStream out = new FileOutputStream(CurrentUser.AVATAR_DIR+"/"+filename);

        byte[] b = new byte[10240];//容量为10240字节的缓存区
        int len = -1;//从输入流读取并放入缓存区的数据量

        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.flush();
        out.close();
        in.close();

        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        userService.setAvatar(currentUser,filename);

        return Result.success();
    }

    @PostMapping("/change")
    public Result setUserData(@RequestBody rUser user){
        if(userService.nameClash(user.getU_name())){
            return Result.success("该昵称已存在");
        }else{
            userService.setNowUser(user);
            return Result.success();
        }
    }
}
